using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using MainGame.GamePlay;

namespace MainGame.Menu
{


  public interface IMenuPause
  {
    event MenuEvent OnMPBackClicked;
    event MenuEvent OnMPBackToMenu;
    event MenuEvent OnNewGame;
    event MenuEvent OnMPBackToMenuTooEarly;
  }

  public class MenuPause : BaseMenu, IMenuPause
  {

    Button BackBtn, NewGame, BackMenuBtn, ShadowBox, BackShadow, NewGameShadow, tutorialCheckBox, hintsCheckBox;
    Vector2 savedPos;
    public int Direction = -1, Direction2 = 1, move = 0, FloatingRange = 0, currentTime = 0;
    public bool Animated = false, hintsChecked, tutorialChecked;
    SpriteBatch spriteBatch;

    public MenuPause(Game game, MenuDepths depth)
      : base(game, depth)
    {
      background = game.Content.Load<Texture2D>(@"Images//Menu//HelpBg2");
      bounds = new Rectangle(-game.GraphicsDevice.Viewport.Width, 0, game.GraphicsDevice.Viewport.Width + 50, game.GraphicsDevice.Viewport.Height);
      game.Services.AddService(typeof(IMenuPause), this);
    }

    protected override void Dispose(bool disposing)
    {
      Game.Services.RemoveService(typeof(IMenuPause));
      base.Dispose(disposing);
    }

    int idbutton = 0;
    public override void Initialize()
    {
      savedPos.X = bounds.X;
      spriteBatch = Game.GetContext().SpriteBatch;
      // Load all buttons
      Color color = Color.White;
      color.A = 150;

      Color color2 = Color.White;
      color2.A = 200;

      BackShadow = new Button(Game, @"Images//Game//roundedBox", new Rectangle((int)bounds.X + 535, 280, 220, 85), Color.Transparent, "baveuse_small", Locale.Strings.GameWillBeLost.Replace("\\n", "\n"), Color.Black, 0.8f);
      AddButton(BackShadow, () =>
      { });

      NewGameShadow = new Button(Game, @"Images//Game//roundedBox", new Rectangle((int)bounds.X + 45, 280, 220, 85), Color.Transparent, "baveuse_small", Locale.Strings.GameWillBeSaved.Replace("\\n", "\n"), Color.Black, 0.8f);
      AddButton(NewGameShadow, () =>
      { });

      ShadowBox = new Button(Game, @"Images//Game//roundedBox", new Rectangle((int)bounds.X + 95, 305, 150, 85), new Color(0, 0, 0, 150), "baveuse_small", "", Color.Transparent);
      AddButton(ShadowBox, () =>
      { });

      BackBtn = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 580, 80, 150, 85), Color.Green, "baveuse1_font", Locale.Strings.SaveResume.Replace("\\n", "\n"), color2, 0.7f);
      NewGame = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 580, 280, 150, 85), Color.Red, "baveuse1_font", Locale.Strings.NewGame.Replace("\\n", "\n"), color2, 0.7f);
      BackMenuBtn = new Button(Game, @"Images//Menu//button2", new Rectangle((int)bounds.X + 90, 280, 150, 85), Color.Blue, "baveuse1_font", Locale.Strings.BackToMain.Replace("\\n", "\n"), color2, 0.6f);
     
      AddButton(NewGame, () =>
      {
        NewGame.touched = true;
        Animated = true;
        Direction = 1;
        idbutton = 1;
      });

      
      AddButton(BackMenuBtn, () =>
      {
        var ui_renderer = Game.Services.GetService(typeof(IUIRenderer)) as IUIRenderer;
        currentTime = (int)ui_renderer.elapsedTime;
        
          BackMenuBtn.touched = true;
      });

      AddButton(BackBtn, () =>
      {
        BackBtn.touched = true;
        var ui_renderer = Game.Services.GetService(typeof(IUIRenderer)) as UIRenderer;
        foreach (ClickableButton cb in ui_renderer.buttons)
          if (cb.b.color == Color.Red)
            cb.b.color = Color.White;

        Animated = true;
        Direction = 1;
        idbutton = 3;
      });

      hintsCheckBox = new Button(Game, @"Images//Menu//CheckBox", new Rectangle((int)bounds.X + 120, (int)bounds.Y + 50, 35, 35), Color.White, "large_font", "", Color.DarkBlue);
      AddButton(hintsCheckBox, () =>
      {
        if (!hintsChecked)
          hintsChecked = true;
        else
          hintsChecked = false;
        Game.GetContext().currentConfig.hints = hintsChecked;
        var boardRenderer = Game.Services.GetService(typeof(IBoardRenderer)) as BoardRenderer;
        boardRenderer.Hints = hintsChecked;
      });


      tutorialCheckBox = new Button(Game, @"Images//Menu//CheckBox", new Rectangle((int)bounds.X + 120, (int)bounds.Y + 140, 35, 35), Color.White, "large_font", "", Color.DarkBlue);
      AddButton(tutorialCheckBox, () =>
      {
        if (!tutorialChecked)
          tutorialChecked = true;
        else
          tutorialChecked = false;
        Game.GetContext().currentConfig.tutorial = tutorialChecked;
        var boardRenderer = Game.Services.GetService(typeof(IBoardRenderer)) as BoardRenderer;
        boardRenderer.Tutorial = tutorialChecked;
      });

      // Load current settings
      hintsChecked = Game.GetContext().currentConfig.hints;
      tutorialChecked = Game.GetContext().currentConfig.tutorial;

      base.Initialize();
    }

    public override void Update(GameTime gameTime)
    {

      if (BackMenuBtn.touched)
      {
        //Salvataggio partita
        /// bug fix
        //if (currentTime > 3) 
        //{
          BackMenuBtn.touched = true;
          Animated = true;
          Direction = 1;
          idbutton = 2;
        //}        
        //else //Se le carte non sono complete sul tavolo ad inizio partita la partita non viene salvata
        //{
        //  BackMenuBtn.touched = true;
        //  Animated = true;
        //  Direction = 1;
        //  idbutton = 0;       
        //}
      
      }

      if (NewGameShadow.bounds.Y > 330)
      {
        ShadowBox.bounds = new Rectangle((int)bounds.X + 560 + FloatingRange, 85 + FloatingRange, 150, 85);
        BackBtn.bounds = new Rectangle((int)bounds.X + 555 - FloatingRange, 80 - FloatingRange, 150, 85);
      // Input: back button pressed
      if (((Game1)Game).IsBackButtonPressed())
      {
        var ui_renderer = Game.Services.GetService(typeof(IUIRenderer)) as UIRenderer;
        foreach (ClickableButton cb in ui_renderer.buttons)
          if (cb.b.color == Color.Red)
            cb.b.color = Color.White;
        
        Animated = true;
        Direction = 1;
        idbutton = 3;
      }

        move += Direction2;

        if (move == 20)
          Direction2 = -1;

        if (move == 0)
          Direction2 = 1;

        if ((move % 5 == 0) && (Direction2 == 1))
          FloatingRange++;

        if ((move % 5 == 0) && (Direction2 == -1))
          FloatingRange--;
      }
      if (NewGameShadow.bounds.Y < 355)
        NewGameShadow.bounds.Y += 3;

      if (BackShadow.bounds.Y < 355)
        BackShadow.bounds.Y += 3;
      

      if (Animated)
      {

        bounds.X += Direction * 60;

        foreach (Button b in (from cb in buttons select cb.b))
        {
          b.bounds.X += Direction * 60;
        }

        if (bounds.X < ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2) && Direction == -1)
        {
          
          Animated = false;
          int offset = bounds.X ;
          bounds.X = ((Game.GraphicsDevice.Viewport.Width - bounds.Width) / 2);

          foreach (Button b in (from cb in buttons select cb.b))
          {
            b.bounds.X += -Direction * offset;
          }
        }

        if (bounds.X > savedPos.X)
        {
          if (OnMPBackClicked != null && idbutton==3) OnMPBackClicked(); // Torna al Gioco
          
          if (OnMPBackToMenu != null && idbutton==2) OnMPBackToMenu(); // Torna al Menu Principale

          if (OnNewGame != null && idbutton == 1) OnNewGame(); // Nuova Partita

          if (OnMPBackToMenu != null && idbutton == 0) OnMPBackToMenuTooEarly(); // Torna al Menu Principale senza salvare


        }

      }

      base.Update(gameTime);
    }

    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();

      if (hintsChecked)
        spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Menu//Check"), new Rectangle(hintsCheckBox.bounds.Center.X - 16, hintsCheckBox.bounds.Top - 10, 40, 40), Color.Red);

      if (tutorialChecked)
        spriteBatch.Draw(Game.Content.Load<Texture2D>(@"Images//Menu//Check"), new Rectangle(tutorialCheckBox.bounds.Center.X - 16, tutorialCheckBox.bounds.Top - 10, 40, 40), Color.Red);

      spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Hints, new Vector2((int)bounds.X + 165, bounds.Y + 40), Color.Black);
      spriteBatch.DrawString(Game.Content.Load<SpriteFont>("baveuse1_font"), Locale.Strings.Tutorial, new Vector2((int)bounds.X + 165, bounds.Y + 125), Color.Black);


      spriteBatch.End();
    }

    public event MenuEvent OnMPBackClicked;
    public event MenuEvent OnMPBackToMenu;
    public event MenuEvent OnNewGame;
    public event MenuEvent OnMPBackToMenuTooEarly;
  }
}
